package net.sourceforge.m2settings.gui;

import java.awt.GridBagLayout;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import java.awt.GridBagConstraints;
import javax.swing.JTable;
import javax.swing.table.TableModel;

import org.apache.log4j.Logger;

import net.sourceforge.m2settings.model.Settings;

import java.awt.GridLayout;
import java.awt.BorderLayout;

public class JTablePanel extends JPanel {

	private static final long serialVersionUID = 1L;
	private JPanel jPanelButtons = null;
	private JScrollPane jScrollPane = null;
	private JTable jTable = null;
	
	private JButton jButtonEdit = null;
	private JButton jButtonCreate = null;
	private JButton jButtonRemove = null;
	
	private String name="element";  //  @jve:decl-index=0:
	private TableModel tableModel=null;

	private Logger log = Logger.getLogger(JTablePanel.class);  //  @jve:decl-index=0:
	
	/**
	 * This is the default constructor
	 * @param userSettings 
	 * @param dataTableModel 
	 * @param name 
	 */
	public JTablePanel( String name, TableModel dataTableModel) {
		super();
		log.debug("Creating Panel "+name+" with data "+dataTableModel);
		this.name=name;
		tableModel=dataTableModel;

		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setLayout(new BorderLayout());
		this.setSize(300, 200);
		this.add(getJScrollPane(), BorderLayout.CENTER);
		this.add(getJPanelButtons(), BorderLayout.SOUTH);
	}

	/**
	 * This method initializes jPanelButtons	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanelButtons() {
		if (jPanelButtons == null) {
			GridBagConstraints gridBagConstraints = new GridBagConstraints();
			gridBagConstraints.fill = GridBagConstraints.BOTH;
			gridBagConstraints.gridy = -1;
			gridBagConstraints.weightx = 1.0;
			gridBagConstraints.weighty = 1.0;
			gridBagConstraints.gridx = -1;
			jPanelButtons = new JPanel();
			jPanelButtons.setLayout(new GridBagLayout());
			jPanelButtons.add(getJButtonEdit(), new GridBagConstraints());
			jPanelButtons.add(getJButtonCreate(), new GridBagConstraints());
			jPanelButtons.add(getJButtonRemove(), new GridBagConstraints());
		}
		return jPanelButtons;
	}

	/**
	 * This method initializes jScrollPane	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getJScrollPane() {
		if (jScrollPane == null) {
			jScrollPane = new JScrollPane();
			jScrollPane.setViewportView(getJTable());
		}
		return jScrollPane;
	}

	/**
	 * This method initializes jTable	
	 * 	
	 * @return javax.swing.JTable	
	 */
	private JTable getJTable() {
		if (jTable == null) {
			jTable = new JTable();
			jTable.setModel(tableModel);
		}
		return jTable;
	}
	/**
	 * This method initializes jButtonEdit	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonEdit() {
		if (jButtonEdit == null) {
			jButtonEdit = new JButton();
			jButtonEdit.setText("Edit");
			jButtonEdit.setToolTipText("Edit the selected "+name);
		}
		return jButtonEdit;
	}

	/**
	 * This method initializes jButtonCreate	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonCreate() {
		if (jButtonCreate == null) {
			jButtonCreate = new JButton();
			jButtonCreate.setText("Create");
			jButtonCreate.setToolTipText("Add a new "+name);
			jButtonCreate.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					log.debug("Create actionPerformed()"); 
					//TODO
				}
			});
		}
		return jButtonCreate;
	}

	/**
	 * This method initializes jButtonRemove	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonRemove() {
		if (jButtonRemove == null) {
			jButtonRemove = new JButton();
			jButtonRemove.setText("Remove");
			jButtonRemove.setToolTipText("Delete the selected "+name);
		}
		return jButtonRemove;
	}
}
